package com.woniuxy.cardservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.carddao.mapper.CountCardMapper;
import com.woniuxy.carddao.model.CountCard;
import com.woniuxy.cardservice.exception.CardException;
import com.woniuxy.cardservice.service.CountCardService;
import com.woniuxy.core.status.CardState;
import com.woniuxy.model.dto.CountCardDto;
import com.woniuxy.model.form.AddCountCardForm;
import com.woniuxy.model.form.UpdateCountCardForm;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


/**
 * <p>
 * 次卡会员表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class CountCardServiceImpl extends ServiceImpl<CountCardMapper, CountCard> implements CountCardService {

    /**
     * 商家端新增次卡
     * @param form
     * @param venueId
     * @return
     */
    @Override
    public CountCardDto addCountCard(AddCountCardForm form, Integer venueId) {
        boolean exists = new LambdaQueryChainWrapper<CountCard>(getBaseMapper())
                .eq(CountCard::getCardName, form.getCardName()).exists();
        if (exists) throw new CardException(80001,"次卡名称重复");
        CountCard countCard = BeanUtil.toBean(form, CountCard.class);
        countCard.setCardState(CardState.IS_ENABLE.getState());
        countCard.setVenueId(venueId);
        save(countCard);
        CountCardDto dto = BeanUtil.toBean(countCard, CountCardDto.class);
        return dto;
    }

    /**
     * 商家端禁用次卡
     * @param cardId
     * @param venueId
     */
    @Override
    public void disabledCountCard(Integer cardId, Integer venueId) {
        CountCard countCard = getById(cardId);
        if (ObjectUtils.isEmpty(countCard)) throw new CardException(80002,"次卡不存在");
        if (countCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家的次卡");
        if (countCard.getCardState()==CardState.IS_DISABLED.getState()) throw new CardException(80004,"次卡已禁用，重复操作");
        countCard.setCardState(CardState.IS_DISABLED.getState());
        updateById(countCard);
    }

    /**
     * 商家启用次卡
     * @param cardId
     * @param venueId
     */
    @Override
    public void enableCountCard(Integer cardId, Integer venueId) {
        CountCard countCard = getById(cardId);
        if (ObjectUtils.isEmpty(countCard)) throw new CardException(80002,"次卡不存在");
        if (countCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家的次卡");
        if (countCard.getCardState()==CardState.IS_ENABLE.getState()) throw new CardException(80004,"次卡已启用，重复操作");
        countCard.setCardState(CardState.IS_ENABLE.getState());
        updateById(countCard);
    }

    /**
     * 根据商家id查找次卡
     * @param venueId
     * @return
     */
    @Override
    public List<CountCardDto> findByVenueId(Integer venueId) {
        List<CountCard> list = list(new LambdaQueryWrapper<CountCard>()
                .eq(CountCard::getVenueId, venueId));
        List<CountCardDto> dtos = BeanUtil.copyToList(list, CountCardDto.class);
        return dtos;
    }

    /**
     * 根据id查找次卡成功
     * @param cardId
     * @return
     */
    @Override
    public CountCardDto findById(Integer cardId) {
        CountCard countCard = getById(cardId);
        CountCardDto dto = BeanUtil.toBean(countCard, CountCardDto.class);
        return dto;
    }

    /**
     * 商家端修改次卡信息
     * @param form
     * @param venueId
     */
    @Override
    public void updateCountCard(UpdateCountCardForm form, Integer venueId) {
        CountCard countCard = getById(form.getCardId());
        if (null==countCard) throw new CardException(80002,"次卡不存在");
        if (countCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家的次卡");
        CountCard one = new LambdaQueryChainWrapper<CountCard>(getBaseMapper())
                .eq(CountCard::getCardName, form.getCardName()).one();
        if (null !=one && one.getCardId()!=form.getCardId()) throw new CardException(80001,"次卡名称重复");
       countCard.setCardName(form.getCardName());
       countCard.setCardCount(form.getCardCount());
       countCard.setCardPrice(form.getCardPrice());
       updateById(countCard);
    }
}
